/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conceitosgame.util;

import java.io.IOException;

/**
 *
 * @author jeferson
 */

/*
 * Essa classe contém todos os metedos utilizados para converter de byte para
 * inteiro, de inteiro para byte e de byte em unsigner byte
 */
public class Conversoes {

    public static int byteArray2Int4(byte[] b) {
        return (b[3] << 24)
                + ((b[2] & 0xFF) << 16)
                + ((b[1] & 0xFF) << 8)
                + (b[0] & 0xFF);
    }

    public static int byteArray2Int2(byte[] b) {
        return ((b[1] & 0xFF) << 2)
                + (b[0] & 0xFF);
    }

    @SuppressWarnings("ShiftOutOfRange")
    public static long byteArray2long(byte[] b) {
        return (b[0] << 56)
                + ((b[1] & 0xFF) << 48)
                + ((b[2] & 0xFF) << 40)
                + ((b[3] & 0xFF) << 32)
                + ((b[4] & 0xFF) << 24)
                + ((b[5] & 0xFF) << 16)
                + ((b[6] & 0xFF) << 8)
                + (b[7] & 0xFF);
    }

    public static short byteArray2Short(byte[] b) {
        return (short) ((b[1] << 2)
                + (b[0] & 0xFF));
    }

    public static byte unsignerByte(byte b) {
        return (byte) (b + 128);
    }

    public static byte[] intToByteArray(int value) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            int offset = (b.length - 1 - i) * 8;
            b[i] = (byte) ((value >>> offset) & 0xFF);
        }
        return b;
    }

    public static byte[] toBytes(int i) {
        byte[] result = new byte[4];

        result[0] = (byte) (i);
        result[1] = 0;


        return result;
    }
}
